package com.tzy.rpc.hook;/**
 * @description:
 * @Author HP
 * @create 2022/3/18 15:11
 */

import com.tzy.rpc.factory.ThreadPoolFactory;
import com.tzy.rpc.util.NacosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author HP
 * @create 2022-03-18 15:11
 * @desc
 **/
public class ShutdownHook {
    private static final Logger logger = LoggerFactory.getLogger(ShutdownHook.class);

    /**
     * 单例模式创建狗钩子，保证全局只有一个钩子
     */
    private static final ShutdownHook shutdownHook = new ShutdownHook();

    public static ShutdownHook getShutdownHook() {
        return shutdownHook;
    }

    //注销服务的钩子
    public void addClearAllHook() {
        logger.info("关闭后将自动注销所有服务");
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            NacosUtil.clearRegistry();
            ThreadPoolFactory.shutDownAll();
        }));
    }
}
